package com.gl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gl.entity.EduCourse;
import com.gl.entity.EduCourseDescription;
import com.gl.entity.EduTeacher;
import com.gl.entity.course.CourseDescription;
import com.gl.entity.course.CoursePublish;
import com.gl.entity.course.CourseQuery;
import com.gl.service.EduCourseDescriptionService;
import com.gl.service.EduCourseService;
import com.gl.service.EduTeacherService;
import com.gl.utils.CommonResponse;
import com.gl.utils.ConstantVodUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-14
 */
@RestController
@RequestMapping("/course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduTeacherService teacherService;

    @Autowired
    private EduCourseDescriptionService descriptionService;
    //首页搜索框查询
    @GetMapping("courseSearch/{title}")
    public CommonResponse courseSearch(@PathVariable String title){
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.like("title",title);
        List<EduCourse> list = courseService.list(wrapper);
        String subjectParentId = list.get(0).getSubjectParentId();//第一个课程的分类一级id
        List<CourseDescription> data = new ArrayList<>();
        for (EduCourse eduCourse : list) {
            CourseDescription eduCourseDescription = new CourseDescription();
            //根据课程ID查询相关课程描述信息
            EduCourseDescription description = descriptionService.getById(eduCourse.getId());
            if (description != null){
                eduCourseDescription.setDescription(description.getDescription());
            }
            BeanUtils.copyProperties(eduCourse,eduCourseDescription);
            data.add(eduCourseDescription);
        }
        return CommonResponse.ok().data("list",data).data("subjectId",subjectParentId);
    }


    //推荐课程
    @GetMapping("recommand/{subjectId}")
    public CommonResponse recommand(@PathVariable String subjectId){
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("subject_parent_id",subjectId);
        wrapper.eq("price",0);
        List<EduCourse> list = courseService.list(wrapper);

        return CommonResponse.ok().data("list",list);
    }


    //分页按条件查询课程
    @PostMapping("pageCourse/{current}/{pageSize}")
    public Map pageCourse(@PathVariable long current,
                            @PathVariable long pageSize,
                            @RequestBody(required = false) EduCourse course){
        Page<EduCourse> page = new Page<>(current,pageSize);
        Map<String,Object> map = courseService.pageCourse(page,course);
        return map;
    }



    //根据课程id查询课程详情
    @GetMapping("detailByCourseId/{courseId}")
    public Map detailByCourseId(@PathVariable String courseId){
        Map<String,Object> map = courseService.detailByCourseId(courseId);
        return map;
    }



    //后台-----------------------------------------------------------------------------------------------

    //添加课程
    @PostMapping("saveCourse")
    public CommonResponse saveCourse(@RequestBody CoursePublish coursePublish){
        //添加课程，返回课程id
        String courseId = courseService.saveCourse(coursePublish);
        return CommonResponse.ok().data("courseId",courseId);
    }

    //根据课程id查询课程基本信息
    @GetMapping("getBaseInfo/{id}")
    public CommonResponse getBaseInfo(@PathVariable String id){
        //查询课程信息
        EduCourse course = courseService.getById(id);
        //查询描述表信息
        EduCourseDescription description = descriptionService.getById(id);
        //将查询出来的信息封装到自定义课程类中
        CoursePublish coursePublish = new CoursePublish();
        BeanUtils.copyProperties(course,coursePublish);
        if(description!=null){
            coursePublish.setDescription(description.getDescription());
        }
        return CommonResponse.ok().data("course",coursePublish);
    }


    //修改课程基本信息
    @PostMapping("updateCourse")
    public CommonResponse updateCourse(@RequestBody CoursePublish coursePublish){
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(coursePublish,course);
        //修改课程
        courseService.updateById(course);
        //修改课程简介
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(course.getId());
        courseDescription.setDescription(coursePublish.getDescription());
        descriptionService.updateById(courseDescription);
        return CommonResponse.ok();
    }

    //课程发布时根据课程id查询最终信息
    @GetMapping("getFinalInfoByCourseId/{id}")
    public CommonResponse getFinalInfoByCourseId(@PathVariable String id){
        CoursePublish coursePublish = courseService.getFinalInfoByCourseId(id);

        return CommonResponse.ok().data("courseInfo",coursePublish);
    }

    //课程发布
    @GetMapping("publishCourse/{id}")
    public CommonResponse publishCourse(@PathVariable String id){
        //获取课程信息
        EduCourse course = courseService.getById(id);
        //将发布状态改变 Normal:已发布
        course.setStatus("Normal");
        //修改
        courseService.updateById(course);
        return CommonResponse.ok();
    }



    //分页按条件查询课程
    @PostMapping("pageBackCourse/{current}/{limit}")
    public CommonResponse pageBackCourse(@PathVariable long current,@PathVariable long limit, @RequestBody CourseQuery courseQuery){
        Page<EduCourse> page = new Page<>(current,limit);
        //查询条件
        String begin = courseQuery.getBegin();//开始时间
        String end = courseQuery.getEnd();//截至时间
        String status = courseQuery.getStatus();//发布状态
        String title = courseQuery.getTitle();//课程名称
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.ge("gmt_create",end);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        courseService.page(page,wrapper);
        List<EduCourse> courseList = page.getRecords();
        List<CoursePublish> finalCourse = new ArrayList<>();//最终返回的集合
        long total = page.getTotal();
        //将所有课程遍历，根据里面讲师id查询讲师名称，并将所有数据放到自定义的课程里
        for (EduCourse course : courseList) {
            CoursePublish coursePublish = new CoursePublish();
            //根据讲师id查询讲师名称
            EduTeacher teacher = teacherService.getById(course.getTeacherId());
            BeanUtils.copyProperties(course,coursePublish);
            if(teacher != null){
                coursePublish.setTeacherName(teacher.getName());
            }
            finalCourse.add(coursePublish);
        }
        return CommonResponse.ok().data("list",finalCourse).data("total",total);
    }

}

